﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DAL;
namespace BLL
{
    // state : 0 -> duyet , 1 -> chua duyet

    public class ArticleDao
    {
        public int ArticleId { get; set; }
        public string ArticleName { get; set; }
        public string URL { get; set; }
        public string Images { get; set; }
        public string Description { get; set; }
        public string ArticleContent { get; set; }
        public string State { get; set; }
        public DateTime? CreateDate { get; set; }
        public string CategoryName { get; set; }
        public string MetaKeyword { get; set; }
        public int CountComment { get; set; }
        public string PhanLoaiTin { get; set; }
    }
    public class ArticleHelper
    {
        public static void Add_Article(int? CategoryId, string ArticleName, string Images, string MetaKeyword, string MetaDescription, string Description, string ArticleContent, int State, DateTime CreateDate, string Value_config)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            tbl_Article article = new tbl_Article();
            article.CategoryId = CategoryId;
            article.ArticleName = ArticleName;
            article.URL = Util.VietnameseText.ToUpText(ArticleName);
            article.Images = Images;
            article.MetaKeyword = MetaKeyword;
            article.MetaDescription = MetaDescription;
            article.Description = Description;
            article.ArticleContent = ArticleContent;
            article.State = State;
            article.CreateDate = CreateDate;
            article.Value_config = Value_config;
            db.tbl_Articles.InsertOnSubmit(article);
            db.SubmitChanges();
        }
        public static void Update_Article(int ArticleId, int? CategoryId, string ArticleName, string Images, string MetaKeyword, string MetaDescription, string Description, string ArticleContent, int State, DateTime CreateDate, string Value_config)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            tbl_Article article = db.tbl_Articles.SingleOrDefault(k => k.ArticleId == ArticleId);
            article.CategoryId = CategoryId;
            article.ArticleName = ArticleName;
            article.URL = Util.VietnameseText.ToUpText(ArticleName);
            if (Images != null)
            {
                article.Images = Images;
            }
            article.MetaKeyword = MetaKeyword;
            article.MetaDescription = MetaDescription;
            article.Description = Description;
            article.ArticleContent = ArticleContent;
            article.State = State;
            article.Value_config = Value_config;
            article.CreateDate = CreateDate;
            db.SubmitChanges();
        }
        public static void Delete_Article(int ArticleId)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            tbl_Article article = db.tbl_Articles.SingleOrDefault(k => k.ArticleId == ArticleId);
            db.tbl_Articles.DeleteOnSubmit(article);
            db.SubmitChanges();

        }
        public static tbl_Article Get_ArticleId(int ArticleId)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            tbl_Article article = db.tbl_Articles.SingleOrDefault(k => k.ArticleId == ArticleId);
            return article;
        }
        //
        public static void Duyet_Huy_Bai(int ArticleId, int State)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            tbl_Article article = db.tbl_Articles.SingleOrDefault(k => k.ArticleId == ArticleId);
            article.State = State;
            db.SubmitChanges();
        }
        public static List<ArticleDao> List_Article_Admin(int State, string Title, string PhanLoai)
        {
            #region

            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");


            var sql = (from article in db.tbl_Articles
                       //join cate in db.tbl_Categories on article.CategoryId equals cate.CategoryId
                       where article.ArticleName.Contains(Title) && (article.State == State || State == -1) && (PhanLoai == "-1" || article.Value_config == PhanLoai)
                       select new ArticleDao
                       {
                           ArticleId = article.ArticleId,
                           ArticleName = article.ArticleName,
                           URL = article.URL,
                           Images = article.Images,
                           Description = article.Description,
                           ArticleContent = article.ArticleContent,
                           State = article.State == 0 ? "Duyệt" : "Chờ duyệt",
                           CreateDate = article.CreateDate,
                           //CategoryName = cate.CategoryName,
                           MetaKeyword = article.MetaKeyword,
                           PhanLoaiTin = article.Value_config == "TT" ? "Tin tức" : (article.Value_config == "TDCT" ? "Theo dòng cuộc thi" : (article.Value_config == "TVNN" ? "Tư vấn nghề nghiệp" : (article.Value_config == "TVTD" ? "Tư vấn tuyển dụng" : "")))
                       }).OrderByDescending(k => k.CreateDate);
            return sql.ToList();


            #endregion
        }
        public static IQueryable Top_BV_Valueconfig(string PhanLoai, int skip, int tak)
        {
            #region

            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            var sql = (from article in db.tbl_Articles
                       //join cate in db.tbl_Categories on article.CategoryId equals cate.CategoryId
                       where article.Value_config == PhanLoai && article.State == 0
                       select new ArticleDao
                       {
                           ArticleId = article.ArticleId,
                           ArticleName = article.ArticleName,
                           URL = article.URL,
                           Images = article.Images,
                           Description = article.Description,
                           ArticleContent = article.ArticleContent,
                           State = article.State == 0 ? "Duyệt" : "Chờ duyệt",
                           CreateDate = article.CreateDate,
                           //CategoryName = cate.CategoryName,
                           MetaKeyword = article.MetaKeyword,
                       }).OrderByDescending(k => k.CreateDate);
            return sql.Skip(skip).Take(tak);


            #endregion
        }
        //
        //public static List<ArticleDao> List_Article_Category(int CategoryId)
        //{
        //    #region

        //    var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
        //    if (CategoryId == 0)
        //    {
        //        var sql = (from article in db.tbl_Articles
        //                   join cate in db.tbl_Categories on article.CategoryId equals cate.CategoryId
        //                   where article.Value_config == null
        //                   select new ArticleDao
        //                   {
        //                       ArticleId = article.ArticleId,
        //                       ArticleName = article.ArticleName,
        //                       URL = article.URL,
        //                       Images = article.Images,
        //                       Description = article.Description,
        //                       ArticleContent = article.ArticleContent,
        //                       State = article.State == 0 ? "Duyệt" : "Chờ duyệt",
        //                       CreateDate = article.CreateDate,
        //                       CategoryName = cate.CategoryName,
        //                       MetaKeyword = article.MetaKeyword,
        //                       CountComment = CommentHelper.Count_Comment_AritcleId(article.ArticleId),
        //                   }).OrderBy(k => k.CreateDate);
        //        return sql.ToList();
        //    }
        //    else
        //    {
        //        var sql = (from article in db.tbl_Articles
        //                   join cate in db.tbl_Categories on article.CategoryId equals cate.CategoryId
        //                   where article.CategoryId == CategoryId && article.Value_config == null
        //                   select new ArticleDao
        //                   {
        //                       ArticleId = article.ArticleId,
        //                       ArticleName = article.ArticleName,
        //                       URL = article.URL,
        //                       Images = article.Images,
        //                       Description = article.Description,
        //                       ArticleContent = article.ArticleContent,
        //                       State = article.State == 0 ? "Duyệt" : "Chờ duyệt",
        //                       CreateDate = article.CreateDate,
        //                       CategoryName = cate.CategoryName,
        //                       MetaKeyword = article.MetaKeyword,
        //                       CountComment = CommentHelper.Count_Comment_AritcleId(article.ArticleId),
        //                   }).OrderBy(k => k.CreateDate);
        //        return sql.ToList();
        //    }
        //    #endregion
        //}
        // danh sach tat ca cac bai viet

        // comment
        // bai viet trang chu        
        public static int Count_Article_value_config(string Value_config)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            var sql = from article in db.tbl_Articles
                      where article.Value_config == Value_config && article.State == 0
                      select article;
            return sql.Count();
        }

        public static IQueryable List_BV_Valueconfig(string PhanLoai, int Page, int row)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");

            var query = from b in db.tbl_Articles.Where(p => p.Value_config==PhanLoai && p.State==0)
                        orderby b.CreateDate descending
                        select new
                        {
                            b.ArticleId,
                            b.ArticleName,
                            b.URL,
                            b.Images,
                            href = b.Value_config == "TT" ? ("/tin-tuc" + b.ArticleId + "/" + b.URL + ".html") : (b.Value_config == "TDCT" ? ("/theo-dong-cuoc-thi" + b.ArticleId + "/" + b.URL + ".html") : (b.Value_config == "TVNN" ? ("/tu-van-nghe-nghiep" + b.ArticleId + "/" + b.URL + ".html") : (b.Value_config == "TVTD" ? ("/tu-van-tuyen-dung" + b.ArticleId + "/" + b.URL + ".html") : ""))),
                            b.Description,                            
                        };
            return query.Skip(row * (Page - 1)).Take(row);
        }
        public static IQueryable List_TinLienQuan(int bvid,string phanloai,int skip,int take)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            var query = from b in db.tbl_Articles.Where(p => p.Value_config == phanloai && p.State == 0 && p.ArticleId != bvid)
                        orderby b.CreateDate descending
                        select new
                        {
                            b.ArticleId,
                            b.ArticleName,
                            b.URL,
                            b.Images,
                            href = b.Value_config == "TT" ? ("/tin-tuc" + b.ArticleId +"/"+ b.URL + ".html") : (b.Value_config == "TDCT" ? ("/theo-dong-cuoc-thi" + b.ArticleId+"/" + b.URL + ".html") : (b.Value_config == "TVNN" ? ("/tu-van-nghe-nghiep" + b.ArticleId+"/" + b.URL + ".html") : (b.Value_config == "TVTD" ? ("/tu-van-tuyen-dung" + b.ArticleId+"/" + b.URL + ".html") : ""))),
                            b.Description,
                        };
            return query.Skip(skip).Take(take);
        }
    }
}